package com.das.mechanic_base.adapter.login;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.login.SelectStoreBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3SelectStoreAdapter extends RecyclerView.Adapter<X3SelectStoreAdapter.SelectStoreHolder> {

    private Context mContext;

    private List<SelectStoreBean> mList;

    private List<Boolean> bList;

    private int default_brand_id;

    public X3SelectStoreAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
        bList = new ArrayList<>();
    }

    public void changeStore(List<SelectStoreBean> mList,int default_brand_id) {
        this.mList = mList;
        this.bList.clear();
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getBrandId() == default_brand_id) {
                this.bList.add(true);
            }else {
                this.bList.add(false);
            }
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectStoreHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectStoreHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_store_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectStoreHolder holder, int position) {
        if (bList.get(position)) {
            holder.itemView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_store_select));
            holder.tv_set.setTextColor(Color.parseColor("#333333"));
            holder.tv_set.setVisibility(View.VISIBLE);
            holder.iv_icon.setImageResource(R.mipmap.x3_car_select);
        }else {
            holder.itemView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_store_no_select));
            holder.tv_set.setTextColor(Color.parseColor("#999999"));
            holder.tv_set.setVisibility(View.INVISIBLE);
            holder.iv_icon.setImageResource(R.mipmap.x3_car_no_select);
        }

        String s = X3StringUtils.replaceBlank(mList.get(position).getBrandName());
        holder.tv_brand.setText(s);

        List<String> carNumList = mList.get(position).getCarNumList();
        if (X3StringUtils.isListEmpty(carNumList)) {
            holder.tv_car_num.setVisibility(View.GONE);
        }else {
            holder.tv_car_num.setVisibility(View.VISIBLE);
            String carNum = "";
            for (int i = 0; i < carNumList.size(); i++) {
                if (i == 0) {
                    carNum = carNumList.get(i);
                }else {
                    carNum = carNum + ", " + carNumList.get(i);
                }
            }
            holder.tv_car_num.setText(carNum);
        }

        holder.itemView.setOnClickListener(v -> {
            for (int i = 0; i < bList.size(); i++) {
                bList.remove(i);
                bList.add(i,i == position);
            }
            notifyDataSetChanged();
            if (iOnClickStore != null) {
                iOnClickStore.iOnClickStoreSelect(mList.get(position).getBrandId());
            }
        });

    }

    IOnClickStore iOnClickStore;

    public void setiOnClickStore(IOnClickStore iOnClickStore) {
        this.iOnClickStore = iOnClickStore;
    }

    public interface IOnClickStore {
        void iOnClickStoreSelect(long brandId);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectStoreHolder extends RecyclerView.ViewHolder {
        TextView tv_brand;
        TextView tv_car_num;
        TextView tv_set;
        ImageView iv_icon;
        public SelectStoreHolder(@NonNull View itemView) {
            super(itemView);
            tv_brand = itemView.findViewById(R.id.tv_brand);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            tv_set = itemView.findViewById(R.id.tv_set);
            iv_icon = itemView.findViewById(R.id.iv_icon);
        }
    }
}
